package cyy.example.com.facotry.share;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

import com.example.com.common.app.Application;


public class SPEngine {
    private static Context sContext;
    private static SPEngine spEngine = new SPEngine();

    public static SPEngine getSPEngine() {
        return spEngine;
    }

    private SharedPreferences sharedPreferences;
    private Editor editor;
    private boolean enterGuide;  //是否进入过引导页
    private boolean loginState;   //登陆状态
    private boolean isAutoLogin;    //是否自动登陆
    private boolean isRememberPsw;    //是否记住密码
    private boolean isForceUp;    //是否强制更新app  1.强制更新 0不更新
    private boolean haveNewApp;    //是否有更新
    private String mLocationArea;    //定位
    private String mLocationcityCode;    //定位
    private String mQueryConfig;    //污染源查询配置的条件
    private String mAppConfigJson;    //角色类型列表
    private String mAppUrl;    //app更新地址
    private String mNewAppVersionNo;    //新的app版本号
    private long mMapPersonLocationRefreshTime;    //位置刷新间隔
    private long mRealDataRefreshTime;    //图表实时数据刷新间隔
    private long mMyLocationRefreshTime;    //我的为位置刷新时间
    private long mVideoUploadMaxSize;    //视频上传最大值
    private long mBanneLoadTime;    //轮播图加载时间
    private String mCurrentEvnName;    //当前的环境
    private String mBannerList;    //轮播图数据
    private String mRoleId;  //用户权限id
    private String taskDescInput;

    //加载数据
    private void loadInfo() {
        enterGuide = sharedPreferences.getBoolean("enterGuide", false);
        loginState = sharedPreferences.getBoolean("loginState", false);
        isAutoLogin = sharedPreferences.getBoolean("isAutoLogin", false);
        isRememberPsw = sharedPreferences.getBoolean("isRememberPsw", false);
        isForceUp = sharedPreferences.getBoolean("isForceUp", false);
        haveNewApp = sharedPreferences.getBoolean("haveNewApp", false);
        mLocationArea = sharedPreferences.getString("mLocationArea", "");
        mLocationcityCode = sharedPreferences.getString("mLocationcityCode", "");
        mQueryConfig = sharedPreferences.getString("mQueryConfig", "");
        mAppConfigJson = sharedPreferences.getString("mAppConfigJson", "");
        mAppUrl = sharedPreferences.getString("mAppUrl", "");
        mNewAppVersionNo = sharedPreferences.getString("mNewAppVersionNo", "");
        mBannerList = sharedPreferences.getString("mBannerList", "");
        mMapPersonLocationRefreshTime = sharedPreferences.getLong("mMapPersonLocationRefreshTime", 0);
        mRealDataRefreshTime = sharedPreferences.getLong("mRealDataRefreshTime", 0);
        mMyLocationRefreshTime = sharedPreferences.getLong("mMyLocationRefreshTime", 1000 * 60 * 60);//默认时间1分钟
        mVideoUploadMaxSize = sharedPreferences.getLong("mVideoUploadMaxSize", 0);
        mBanneLoadTime = sharedPreferences.getLong("mBanneLoadTime", 0);
        mCurrentEvnName = sharedPreferences.getString("mCurrentEvnName", "");
        mRoleId = sharedPreferences.getString("mRoleId", "");
    }

    //退出账号的时候调用
    public void clearUserInfo() {
        setLoginState(false);
        getUserInfo().clear();
    }

    public String getTaskDescInput() {
        return sharedPreferences.getString("taskDescInput","");
    }

    public void setTaskDescInput(String taskDescInput) {
        this.taskDescInput = taskDescInput;
        editor.putString("taskDescInput", taskDescInput).commit();
    }

    //是否进入过引导页
    public boolean isEnterGuide() {
        return enterGuide;
    }

    public void setEnterGuide(boolean enterGuide) {
        this.enterGuide = enterGuide;
        editor.putBoolean("enterGuide", enterGuide).apply();
    }

    public String getmLocationArea() {
        return mLocationArea;
    }

    public void setmLocationArea(String mLocationArea) {
        this.mLocationArea = mLocationArea;
        editor.putString("mLocationArea", mLocationArea).apply();
    }

    public boolean isHaveNewApp() {
        return haveNewApp;
    }

    public void setHaveNewApp(boolean haveNewApp) {
        this.haveNewApp = haveNewApp;
        editor.putBoolean("haveNewApp", haveNewApp).apply();
    }

    public void setmRoleId(String mRoleId) {
        this.mRoleId = mRoleId;
        editor.putString("mRoleId", mRoleId);
    }

    public String getmRoleId(){
        return sharedPreferences.getString("mRoleId", "");
    }
    public String getCurrentEvnName() {
        return mCurrentEvnName;
    }

    public void setCurrentEvnName(String currentEvnName) {
        mCurrentEvnName = currentEvnName;
        editor.putString("mCurrentEvnName", mCurrentEvnName).apply();
    }

    public String getmLocationcityCode() {
        return mLocationcityCode;
    }

    public void setmLocationcityCode(String mLocationcityCode) {
        this.mLocationcityCode = mLocationcityCode;
        editor.putString("mLocationcityCode", mLocationcityCode).apply();
    }


    public boolean isRememberPsw() {
        return isRememberPsw;
    }

    public void setRememberPsw(boolean rememberPsw) {
        isRememberPsw = rememberPsw;
        editor.putBoolean("isRememberPsw", rememberPsw).apply();
    }


    //是否为登录状态
    public boolean isLoginState() {
        return loginState;
    }

    public void setLoginState(boolean loginState) {
        this.loginState = loginState;
        editor.putBoolean("loginState", loginState).apply();
    }

    public String getBannerList() {
        return mBannerList;
    }

    public void setBannerList(String bannerList) {
        mBannerList = bannerList;
        editor.putString("mBannerList", mBannerList).apply();
    }

    public void putString(String key, String value) {
        editor.putString(key, value).apply();
    }

    public String getString(String key) {
        return sharedPreferences.getString(key, "");
    }

    public void putInt(String key, int value) {
        editor.putInt(key, value).apply();
    }

    public int getInt(String key) {
        return sharedPreferences.getInt(key, 0);
    }

    public boolean isAutoLogin() {
        return isAutoLogin;
    }

    public void setAutoLogin(boolean autoLogin) {
        this.isAutoLogin = autoLogin;
        editor.putBoolean("isAutoLogin", autoLogin).apply();
    }

    public String getQueryConfig() {
        return mQueryConfig;
    }

    public void setQueryConfig(String mQueryConfig) {
        this.mQueryConfig = mQueryConfig;
        editor.putString("mQueryConfig", mQueryConfig).apply();
    }

    public String getAppConfigJso() {
        return mAppConfigJson;
    }

    public void setAppConfigJso(String body) {
        mAppConfigJson = body;
        editor.putString("mAppConfigJson", mAppConfigJson).apply();
    }

    public long getBanneLoadTime() {
        return mBanneLoadTime;
    }

    public void setBanneLoadTime(long banneLoadTime) {
        mBanneLoadTime = banneLoadTime;
        editor.putLong("mBanneLoadTime", mBanneLoadTime).apply();
    }

    public long getMapPersonLocationRefreshTime() {
        return mMapPersonLocationRefreshTime;
    }

    public void setMapPersonLocationRefreshTime(long mapPersonLocationRefreshTime) {
        mMapPersonLocationRefreshTime = mapPersonLocationRefreshTime;
        editor.putLong("mMapPersonLocationRefreshTime", mMapPersonLocationRefreshTime).apply();
    }

    public long getRealDataRefreshTime() {
        return mRealDataRefreshTime;
    }

    public void setRealDataRefreshTime(long realDataRefreshTime) {
        mRealDataRefreshTime = realDataRefreshTime;
        editor.putLong("mRealDataRefreshTime", mRealDataRefreshTime).apply();
    }

    public long getMyLocationRefreshTime() {
        return mMyLocationRefreshTime;
    }

    public void setMyLocationRefreshTime(long myLocationRefreshTime) {
        mMyLocationRefreshTime = myLocationRefreshTime;
        editor.putLong("mMyLocationRefreshTime", mMyLocationRefreshTime).apply();
    }

    public long getVideoUploadMaxSize() {
        return mVideoUploadMaxSize;
    }

    public void setVideoUploadMaxSize(long videoUploadMaxSize) {
        mVideoUploadMaxSize = videoUploadMaxSize;
        editor.putLong("mVideoUploadMaxSize", mVideoUploadMaxSize).apply();
    }

    public boolean isForceUp() {
        return isForceUp;
    }

    public void setForceUp(boolean forceUp) {
        isForceUp = forceUp;
        editor.putBoolean("isForceUp", isForceUp).apply();
    }

    public String getAppUrl() {
        return mAppUrl;
    }

    public void setAppUrl(String appUrl) {
        mAppUrl = appUrl;
        editor.putString("mAppUrl", mAppUrl).apply();
    }

    public String getNewAppVersionNo() {
        return mNewAppVersionNo;
    }

    public void setNewAppVersionNo(String upAppVersionNo) {
        mNewAppVersionNo = upAppVersionNo;
        editor.putString("mNewAppVersionNo", mNewAppVersionNo).apply();
    }

    //初始化share
    private SPEngine() {
        sContext = Application.getInstance();
        sharedPreferences = sContext.getSharedPreferences("commondata", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        loadInfo();
    }

    ;

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private UserInfo userInfo;

    public UserInfo getUserInfo() {
        if (userInfo == null) {
            synchronized (spEngine) {
                if (userInfo == null) {
                    userInfo = new UserInfo();
                }
            }
        }
        return userInfo;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static class UserInfo {
        private UserInfo() {
            userInfoSP = sContext.getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
            userInfoEditor = userInfoSP.edit();
            load();
        }

        private SharedPreferences userInfoSP;
        private Editor userInfoEditor;


        private String user_id;//账号id
        private String user_account;//账号
        private String psw;  //密码
        private String RoleId;//权限
        private String roleName;//角色名称
        private String token;    //TOKEN
        private String setMobileToken;    //真的token
        private String area;//所在城市
        private String latitude;//纬度
        private String longgiude;//经度
        private String mPortrait;//头像
        private String mPhone;//电话
        private String mEmail;//邮箱
        private String mName;//用户昵称
        private String userDetailId;//用户类型
        private String regionCode;//用户所属地区编码
        private String distributing;//派发权限 0 无权限  1有全职
        private boolean mIsVoice;//消息通知是否有声音    0：有（默认）1：没有
        private boolean mIsVibration;//是否振动
        private long uplocationTime;//上传位置的时间间隔
        private int mNewMsg;//新的未接受任务
        private int mNewCheckTask;//新的待审核任务任务
        private int audit;


        private void load() {
            user_id = userInfoSP.getString("user_id", "");
            RoleId = userInfoSP.getString("RoleId", "");
            token = userInfoSP.getString("token", "");
            setMobileToken = userInfoSP.getString("setMobileToken", "");
            area = userInfoSP.getString("area", "");
            user_account = userInfoSP.getString("user_account", "");
            latitude = userInfoSP.getString("latitude", "");
            longgiude = userInfoSP.getString("longgiude", "");
            psw = userInfoSP.getString("psw", "");
            mPortrait = userInfoSP.getString("mPortrait", "");
            userDetailId = userInfoSP.getString("userDetailId", "");
            mPhone = userInfoSP.getString("mPhone", "");
            mEmail = userInfoSP.getString("mEmail", "");
            mName = userInfoSP.getString("mName", "");
            roleName = userInfoSP.getString("roleName", "");
            mIsVoice = userInfoSP.getBoolean("mIsVoice", true);
            mIsVibration = userInfoSP.getBoolean("mIsVibration", true);
            uplocationTime = userInfoSP.getLong("uplocationTime", 0);
            mNewMsg = userInfoSP.getInt("mNewMsg", 0);
            mNewCheckTask = userInfoSP.getInt("mNewCheckTask", 0);
            regionCode = userInfoSP.getString("regionCode", "");
            distributing = userInfoSP.getString("distributing", "");
        }

        public void clear() {
            user_id = null;
            RoleId = null;
            token = null;
            setMobileToken = null;
            area = null;
            latitude = null;
            longgiude = null;
            mPortrait = null;
            userDetailId = null;
            mPhone = null;
            mEmail = null;
            mName = null;
            roleName = null;
            distributing = null;
            mIsVoice = true;
            mIsVibration = true;
            regionCode = "";
            uplocationTime = -1;
            mNewMsg = 0;
            mNewCheckTask = 0;
            //清除sp中的数据
            userInfoEditor.clear().apply();

            //保留无需清除的数据
            if (!SPEngine.getSPEngine().isRememberPsw()) {//暂时性功能，token验证模块完成后，删除该判断
                psw = null;
            } else {
                setPsw(psw);
            }
            setUser_account(user_account);

        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
//            userInfoEditor.putString("token", token).apply();
        }

        public String getSetMobileToken() {
            if(TextUtils.isEmpty(setMobileToken)){
               return userInfoSP.getString("setMobileToken", "");
            }else{
                return setMobileToken;
            }
        }

        public void setSetMobileToken(String setMobileToken) {
            this.setMobileToken = setMobileToken;
            userInfoEditor.putString("setMobileToken", setMobileToken).apply();
        }

        public int getAudit() {
            return userInfoSP.getInt("setAudit", 0);
        }

        public void setAudit(int audit) {
            this.audit = audit;
            userInfoEditor.putInt("setAudit", audit).apply();
        }

        public String getUser_id() {
            return user_id;
        }

        public void setUser_id(String user_id) {
            this.user_id = user_id;
            userInfoEditor.putString("user_id", user_id).apply();
        }

        public String getUser_account() {
            return user_account;
        }

        public void setUser_account(String user_account) {
            this.user_account = user_account;
            userInfoEditor.putString("user_account", user_account).apply();
        }

        public String getDistributing() {
            return distributing;
        }

        public void setDistributing(String distributing) {
            this.distributing = distributing;
            userInfoEditor.putString("distributing", distributing).apply();
        }

        public String getPsw() {
            return psw;
        }

        public void setPsw(String psw) {
            this.psw = psw;
            userInfoEditor.putString("psw", psw).apply();
        }

        public String getRegionCode() {
            return regionCode;
        }

        public void setRegionCode(String regionCode) {
            this.regionCode = regionCode;
            userInfoEditor.putString("regionCode", regionCode).apply();
        }

        public String getRoleId() {
            return RoleId;
        }

        public void setRoleId(String roleId) {
            RoleId = roleId;
            userInfoEditor.putString("roleId", roleId).apply();
        }

        public String getArea() {
            return area;
        }

        public void setArea(String area) {
            this.area = area;
            userInfoEditor.putString("area", area).apply();
        }

        public String getlatitude() {
            return latitude;
        }

        public void setlatitude(String latitude) {
            this.latitude = latitude;
            userInfoEditor.putString("latitude", latitude).apply();
        }

        public String getLonggiude() {
            return longgiude;
        }

        public void setLonggiude(String longgiude) {
            this.longgiude = longgiude;
            userInfoEditor.putString("longgiude", longgiude).apply();
        }

        public String getName() {
            return mName;
        }

        public void setName(String name) {
            mName = name;
            userInfoEditor.putString("mName", mName).apply();
        }

        public long getUplocationTime() {
            return uplocationTime;
        }

        public void setUplocationTime(long uplocationTime) {
            this.uplocationTime = uplocationTime;
            userInfoEditor.putLong("uplocationTime", uplocationTime).apply();
        }

        public String getPortrait() {
            return mPortrait;
        }

        public void setPortrait(String portrait) {
            mPortrait = portrait;
            userInfoEditor.putString("mPortrait", mPortrait).apply();
        }


        public String getPhone() {
            return mPhone;
        }

        public void setPhone(String phone) {
            mPhone = phone;
            userInfoEditor.putString("mPhone", mPhone).apply();
        }

        public String getEmail() {
            return mEmail;
        }

        public void setEmail(String email) {
            mEmail = email;
            userInfoEditor.putString("mEmail", mEmail).apply();
        }

        public boolean isVoice() {
            return mIsVoice;
        }

        public void setVoice(boolean voice) {
            mIsVoice = voice;
            userInfoEditor.putBoolean("mIsVoice", mIsVoice).apply();
        }

        public boolean isVibration() {
            return mIsVibration;
        }

        public void setVibration(boolean vibration) {
            mIsVibration = vibration;
            userInfoEditor.putBoolean("mIsVibration", mIsVibration).apply();
        }

        public int getNewMsg() {
            return mNewMsg;
        }

        public void setNewMsg(int newMsg) {
            mNewMsg = newMsg;
            userInfoEditor.putInt("mNewMsg", mNewMsg).apply();
        }

        public String getRoleName() {
            return roleName;
        }

        public void setRoleName(String roleName) {
            this.roleName = roleName;
            userInfoEditor.putString("roleName", roleName).apply();
        }

        public String getUserDetailId() {
            return userDetailId;
        }

        public void setUserDetailId(String userDetailId) {
            this.userDetailId = userDetailId;
            userInfoEditor.putString("userDetailId", userDetailId).apply();
        }

        public int getNewCheckTask() {
            return mNewCheckTask;
        }

        public void setNewCheckTask(int newCheckTask) {
            mNewCheckTask = newCheckTask;
            userInfoEditor.putInt("newCheckTask", newCheckTask).apply();
        }
    }

}
